package api

import (
	"gitee.com/ling1314/origin-utility/constant"
	"gitee.com/ling1314/origin-utility/guacd"
	"gitee.com/ling1314/origin-utility/model"
	"gitee.com/ling1314/origin-utility/modules/service"
	"github.com/gin-gonic/gin"
)

type guaApi struct{}

func (api guaApi) SendInstruct(c *gin.Context) {
	input := struct {
		Id      int64  `json:"id,string"`
		Msg     string `json:"msg"`
		Title   string `json:"title"`
		MsgType string `json:"msgType"`
	}{}
	if err := c.ShouldBindJSON(&input); err != nil {
		BadWithCode(c, constant.ParamsError)
		return
	}
	if err := service.SessionService.SendInstruct(input.Id, input.Title, input.Msg, input.MsgType); err != nil {
		BadWithError(c, err)
		return
	}
	Ok204(c)
}

func (api guaApi) Logoff(c *gin.Context) {
	input := struct {
		Id int64 `uri:"id"`
	}{}
	if err := c.ShouldBindUri(&input); err != nil {
		BadWithCode(c, constant.ParamsError)
		return
	}
	if err := service.SessionService.Logoff(input.Id); err != nil {
		BadWithError(c, err)
		return
	}
	Ok204(c)
}

func (api guaApi) Break(c *gin.Context) {
	input := struct {
		Id int64 `uri:"id"`
	}{}
	if err := c.ShouldBindUri(&input); err != nil {
		BadWithCode(c, constant.ParamsError)
		return
	}
	if err := service.SessionService.Break(input.Id); err != nil {
		BadWithError(c, err)
		return
	}
	Ok204(c)
}

func (api guaApi) GetSessionPageForAdmin(c *gin.Context) {
	var input model.AdminSearchHostSession
	if err := c.ShouldBindQuery(&input); err != nil {
		BadWithCode(c, constant.ParamsError)
		return
	}
	u := GetCurrentUser(c)
	pr, err := service.SessionService.GetSessionPageForAdmin(u.UserID, input)
	if err != nil {
		BadWithError(c, err)
		return
	}
	Ok(c, pr)
}

func (api guaApi) InsertAppOpenLog(c *gin.Context) {
	input := struct {
		AppId         int64 `json:"appId,string"`
		HostSessionId int64 `json:"hostSessionId,string"`
	}{}
	if err := c.ShouldBindJSON(&input); err != nil {
		BadWithCode(c, constant.ParamsError)
		return
	}
	if err := service.SessionService.InsertAppOpenLog(input.HostSessionId, input.AppId); err != nil {
		BadWithError(c, err)
		return
	}
	Ok204(c)
}

func (api guaApi) BindWinSessionId(c *gin.Context) {
	input := struct {
		HostSessionId int64 `json:"hostSessionId,string"`
		WinSessionId  int   `json:"winSessionId,string"`
	}{}
	if err := c.ShouldBindJSON(&input); err != nil {
		BadWithCode(c, constant.ParamsError)
		return
	}
	if err := service.SessionService.BindWinSessionId(input.HostSessionId, input.WinSessionId); err != nil {
		BadWithError(c, err)
		return
	}
	Ok204(c)
}

func (api guaApi) CreateSession(c *gin.Context) {
	input := model.CheckSessionReuseInput{}
	if err := c.ShouldBindQuery(&input); err != nil {
		BadWithCode(c, constant.ParamsError)
		return
	}
	u := GetCurrentUser(c)
	rs, err := service.SessionService.CreateSession(u.UserID, input.AppId)
	if err != nil {
		BadWithError(c, err)
		return
	}
	Ok(c, rs)
}

func (api guaApi) CheckSessionReuse(c *gin.Context) {
	input := model.CheckSessionReuseInput{}
	if err := c.ShouldBindQuery(&input); err != nil {
		BadWithCode(c, constant.ParamsError)
		return
	}
	u := GetCurrentUser(c)
	rs, err := service.SessionService.CheckSessionReuse(u.UserID, input.AppId)
	if err != nil {
		BadWithError(c, err)
		return
	}
	Ok(c, rs)
}

func (api guaApi) WSConnect(c *gin.Context) {
	var guaApi = new(guacd.GuacamoleApi)
	err := guaApi.Guacamole(c)
	if err != nil {
		BadWithCode(c, constant.GuacdError)
		return
	}
	//Ok204(c)
}

func (api guaApi) Monitor(c *gin.Context) {
	var guaApi = new(guacd.GuacamoleApi)
	err := guaApi.GuacamoleMonitor(c)
	if err != nil {
		BadWithCode(c, constant.GuacdError)
		return
	}
	//Ok204(c)
}

func (api guaApi) Resize(c *gin.Context) {
	params := struct {
		Width     string `json:"width" form:"width"`
		Height    string `json:"height" form:"height"`
		SessionId int64  `json:"session_id,string" form:"session_id,string"`
	}{}
	if err := c.ShouldBindQuery(params); err != nil {
		BadWithCode(c, constant.ParamsError)
		return
	}
	err := service.SessionService.Resize(params.Width, params.Height, params.SessionId)
	if err != nil {
		BadWithCode(c, constant.ErrorUnknown)
		return
	}
	Ok204(c)
}
